from django.shortcuts import render
from django.shortcuts import render
from rest_framework.response import Response

from like.models import Like_Comment, Like_Comment_Comment
from .models import Comment, Comment_Comment
from .serializer import CommentSerializer, CommentDetailSerializer, Comment_CommentDetailSerializer, \
    Comment_CommentSerializer
from rest_framework import status, viewsets


# Create your views here.

class CommentViewSet(viewsets.ModelViewSet):
    serializer_class = CommentSerializer

    def get_queryset(self):
        queryset = Comment.objects.all()
        params = self.request.query_params
        if params:
            filters = {}
            for key, value in params.items():
                filters[key] = value
            queryset = queryset.filter(**filters)
        return queryset


class CommentDetailViewSet(viewsets.ModelViewSet):
    serializer_class = CommentDetailSerializer

    def get_queryset(self):
        queryset = Comment.objects.all()
        params = self.request.query_params
        if params:
            filters = {}
            for key, value in params.items():
                filters[key] = value
            queryset = queryset.filter(**filters)
        return queryset

    def get_comment(self, request, *args, **kwargs):
        video_id = kwargs.get('video_id')
        num = kwargs.get('num')
        user_id = kwargs.get('user_id')
        already = kwargs.get('already')
        option = kwargs.get('option')
        if already != '&':
            already_num = [int(num) for num in already.split('&')]
        else:
            already_num = []
        new_already_num = []

        index = 0
        result = []
        mid = []
        if option == 0:
            # 按时间排序
            queryset = Comment.objects.filter(video_id=video_id).order_by('-time')
            for comment in queryset:
                if index >= num:
                    break
                if comment.cid in already_num:
                    continue
                new_already_num.append(comment.cid)
                likes = Like_Comment.objects.filter(comment_id=comment.cid).filter(is_active=True).filter(
                    type=True).count()
                unlikes = Like_Comment.objects.filter(comment_id=comment.cid).filter(is_active=True).filter(
                    type=False).count()
                is_liked = len(
                    Like_Comment.objects.filter(comment_id=comment.cid).filter(is_active=True).filter(type=True).filter(
                        user_id=user_id)) != 0
                is_unliked = len(
                    Like_Comment.objects.filter(comment_id=comment.cid).filter(is_active=True).filter(
                        type=False).filter(
                        user_id=user_id)) != 0
                result_comment = CommentDetailSerializer(comment).data
                result_comment.update(is_liked=is_liked, is_unliked=is_unliked, like_count=likes, unlike_count=unlikes)
                result.append(result_comment)
                index = index + 1

            return Response({'comments': result, 'count': len(queryset), 'already': new_already_num},
                            status=status.HTTP_200_OK)
        else:
            queryset = Comment.objects.filter(video_id=video_id).order_by('time')
            for comment in queryset:
                # 首先计算热度权重
                likes = Like_Comment.objects.filter(comment_id=comment.cid).filter(is_active=True).filter(
                    type=True).count()
                unlikes = Like_Comment.objects.filter(comment_id=comment.cid).filter(is_active=True).filter(
                    type=False).count()
                is_liked = len(
                    Like_Comment.objects.filter(comment_id=comment.cid).filter(is_active=True).filter(type=True).filter(
                        user_id=user_id)) != 0
                is_unliked = len(
                    Like_Comment.objects.filter(comment_id=comment.cid).filter(is_active=True).filter(
                        type=False).filter(
                        user_id=user_id)) != 0
                answers = Comment_Comment.objects.filter(comment_id=comment.cid).count()
                hot = is_liked * 2 + is_unliked * (-1) + answers * 4
                result_comment = CommentDetailSerializer(comment).data
                result_comment.update(is_liked=is_liked, is_unliked=is_unliked, like_count=likes, unlike_count=unlikes,
                                      hot=hot)
                mid.append(result_comment)
            # 按热度排序
            mid.sort(key=lambda x: x['hot'], reverse=True)

            for final_comment in mid:
                if index >= num:
                    break
                if final_comment.get('cid') in already_num:
                    continue
                index = index+1
                new_already_num.append(final_comment.get('cid'))
                result.append(final_comment)
            return Response({'comments': result, 'count': len(queryset), 'already': new_already_num},
                            status=status.HTTP_200_OK)


class Comment_CommentViewSet(viewsets.ModelViewSet):
    serializer_class = Comment_CommentSerializer

    def get_queryset(self):
        queryset = Comment_Comment.objects.all()
        params = self.request.query_params
        if params:
            filters = {}
            for key, value in params.items():
                filters[key] = value
            queryset = queryset.filter(**filters)
        return queryset


class Comment_CommentDetailViewSet(viewsets.ModelViewSet):
    serializer_class = Comment_CommentDetailSerializer

    def get_queryset(self):
        queryset = Comment_Comment.objects.all()
        params = self.request.query_params
        if params:
            filters = {}
            for key, value in params.items():
                filters[key] = value
            queryset = queryset.filter(**filters)
        return queryset

    def get_comment_comment(self, request, *args, **kwargs):
        comment_id = kwargs.get('comment_id')
        user_id = kwargs.get('user_id')
        queryset = Comment_Comment.objects.filter(comment_id=comment_id).order_by('time')
        result = []
        for comment in queryset:
            likes = Like_Comment_Comment.objects.filter(comment_comment_id=comment.ccid).filter(is_active=True).filter(
                type=True).count()
            unlikes = Like_Comment_Comment.objects.filter(comment_comment_id=comment.ccid).filter(
                is_active=True).filter(
                type=False).count()
            is_liked = len(
                Like_Comment_Comment.objects.filter(comment_comment_id=comment.ccid).filter(is_active=True).filter(
                    type=True).filter(
                    user_id=user_id)) != 0
            is_unliked = len(
                Like_Comment_Comment.objects.filter(comment_comment_id=comment.ccid).filter(is_active=True).filter(
                    type=False).filter(
                    user_id=user_id)) != 0
            result_comment = Comment_CommentDetailSerializer(comment).data
            result_comment.update(is_liked=is_liked, is_unliked=is_unliked, like_count=likes, unlike_count=unlikes)
            result.append(result_comment)

        return Response({'answers': result, 'count': len(queryset)},
                        status=status.HTTP_200_OK)
